package org.springblade.common.exception;

import lombok.Getter;
import org.springblade.core.tool.api.IResultCode;
import org.springblade.core.tool.api.ResultCode;

/**
 * 保留版权 : CopyRright (c) 2024-2024 于继业 版权所有
 * 文件名称 : BizException
 * 文件编号 : 1-1-1
 * 文件版本 : 2024-04-14.01
 * 创建日期 : 2024-04-14
 * 修改人名 :
 * 修改日期 :
 * 功能描述 :
 *
 * @author 于继业
 */
@Getter
public class BizException extends RuntimeException {
	/**
	 * 错误代码
	 */
	private final IResultCode resultCode;

	public BizException(String message) {
		super(message);
		this.resultCode = ResultCode.FAILURE;
	}

	public BizException(IResultCode resultCode) {
		super(resultCode.getMessage());
		this.resultCode = resultCode;
	}


	public BizException(IResultCode resultCode, Throwable cause) {
		super(cause);
		this.resultCode = resultCode;
	}

	public static BizException build(IResultCode resultCode) {
		throw new BizException(resultCode);
	}

	public static BizException build(String errorMessage) {
		throw new BizException(errorMessage);
	}

	/**
	 * 抛出异常
	 *
	 * @param resultCode 业务异常的信息
	 */
	public static void throwEx(IResultCode resultCode) {
		throw BizException.build(resultCode);
	}

	public static void throwEx(String errorMessage) {
		throw BizException.build(errorMessage);
	}
}
